﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Gmantis.Controls.Chart;

namespace Gmantis.Controls.Test
{
    public partial class Chart : UserControl
    {
        bool loaded = false;
        TransformGroup tg;
        SampleData sampleData;

        public Chart()
        {
            InitializeComponent();

            sampleData = new SampleData(DataSeries_Loaded);

            tg = new TransformGroup();
            tg.Children.Add(new ScaleTransform() { ScaleX = 0, ScaleY = 0.3 });
            chart.Data = sampleData.DefaultData;
        }

        public ChartType ChartType
        {
            get { return chart.ChartType; }
            set
            {
                if (value != chart.ChartType)
                {
                    // several chart requires special data
                    if (value == ChartType.HighLowOpenClose || value == ChartType.Candle)
                        chart.Data = sampleData.FinancialData;
                    else if (value == ChartType.Bubble)
                        chart.Data = sampleData.BubbleData;
                    else if (value == ChartType.Gantt)
                        chart.Data = sampleData.GanttData;
                    else
                        chart.Data = sampleData.DefaultData;

                    StartAnimation();
                    chart.ChartType = value;
                }
            }
        }

        public Palette Palette
        {
            get { return chart.Palette; }
            set
            {
                StartAnimation();
                chart.Palette = value;
            }
        }

        public ChartTheme Theme
        {
            get { return chart.Theme; }
            set
            {
                StartAnimation();
                chart.Theme = value;
                chart.Palette = Gmantis.Controls.Chart.Palette.Default;
            }
        }

        private void StartAnimation()
        {
            if (loaded)
            {
                ((ScaleTransform)tg.Children[0]).ScaleX = 0;
                ((ScaleTransform)tg.Children[0]).ScaleY = 0.3;
                Storyboard sb = (Storyboard)Resources["sb"];
                sb.Stop();
                Storyboard.SetTarget(sb, tg);
                sb.Begin();
            }
        }

        private void chart_Loaded(object sender, RoutedEventArgs e)
        {
            loaded = true;
            StartAnimation();
        }

        private void DataSeries_Loaded(object sender, EventArgs e)
        {
            // DataSeries.Loaded fires whenever plot element related
            // to the series is loaded
            PlotElement pe = sender as PlotElement;
            if (pe != null)
            {
                pe.RenderTransform = tg;
                pe.RenderTransformOrigin = new Point(0.5, 0.5);

                pe.MouseEnter += new MouseEventHandler(pe_MouseEnter);
                pe.MouseLeave += new MouseEventHandler(pe_MouseLeave);
            }
        }

        void pe_MouseLeave(object sender, MouseEventArgs e)
        {
            PlotElement pe = sender as PlotElement;
            if (pe != null)
            {
                Canvas.SetZIndex(pe, 0);
                Storyboard sb = (Storyboard)Resources["sbEnter"];
                sb.Stop();

                sb = (Storyboard)Resources["sbLeave"];
                sb.Stop();
                Storyboard.SetTarget(sb, pe.Shape);
                sb.Begin();

                pe.Shape.OpacityMask = null;
            }
        }

        void pe_MouseEnter(object sender, MouseEventArgs e)
        {
            PlotElement pe = sender as PlotElement;
            if (pe != null)
            {
                pe.Shape.OpacityMask = (Brush)Resources["mask"];

                if (!(pe is Area))
                    Canvas.SetZIndex(pe, 1);

                Storyboard sb = (Storyboard)Resources["sbEnter"];
                sb.Stop();
                Storyboard.SetTarget(sb, pe.Shape);
                sb.Begin();
            }
        }
    }
}
